layui.use(['form', 'table', 'layer', 'comExt'], function (form, table, layer, comExt) {

    form.render();

    //获取表单数据
    searchCondition = "";

    function getSearchCodition() {
        return searchCondition;
    }

    var userTable = table.render({
        elem: '#userList',
        url: '../user/all',
        height: 460,
        cellMinWidth: 95,
        page: true,
        // limits: [10, 15, 20, 25],
        limit: 10,
        loading: true,
        contentType: "application/json;charset=utf-8",
        id: "userListTable",
        method: 'post',
        where: getSearchCodition(),
        //res 即为原始返回的数据
        parseData: function (res) {
            console.log(res);
            return {
                "code": res.code,
                "count": res.count,
                "data": res.data
            };
        },
        done: function (res, curr, count) {
            $(".layui-laypage-limits").hide();
            if (curr > 1 && null == res.data) {
                curr = curr - 1;
                table.reload('paper-info', {
                    //设定异步数据接口的额外参数，任意设
                    where: {},
                    page: {
                        curr: curr
                    }
                });
            }
        },
        cols: [[
            {type: "radio", fixed: "left", width: 50},
            {field: 'id', title: 'id', minWidth: 50, align: "center", hide: true},
            {field: 'username', title: '用户名', width: 100, minWidth: 100, align: "center"},
            {field: 'loginTime', title: '登录时间', minWidth: 149, align: "center"}
        ]]
    });

    //重新加载表格
    function reload() {
        //组装查询条件
        searchCondition = {
            searchCondition: {
                search: $("#search").val()
            }
        };
        //重新加载表格
        userTable.reload({
            where: searchCondition,
            page: {
                curr: 1  //重新从第一页开始加载
            }
        });
    }

    //查询按钮监听
    form.on("submit(querySubmit)", function () {
        reload();
    });

    //查看按钮监听
    $(".lookUser").click(function () {
        //判断当前所选数据的数目
        var checkStatus = table.checkStatus('userListTable');
        var length = checkStatus.data.length;
        if (length == 0) {
            layer.msg("请选择一条需要查看的内容", {time: 1000});
            return;
        }
        var data = checkStatus.data[0];
        //屏幕可用工作区宽
        const clientWidth = window.screen.availWidth;
        const wid = clientWidth + "px";
        //屏幕可用工作区高
        const clientHeight = window.screen.availHeight;
        const hei = clientHeight + "px";
        var index = layer.open({
            title: "查看用户",
            type: 2,
            anim: -1,
            area: [wid, hei],
            content: "personal-show.html",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                $(document).find("input[type=radio]").val(data.role);
                comExt.fillInput(body, data);
            }
        });
        comExt.full(index);
        /* 渲染表单 */
        form.render();
    });

    //删除按钮监听
    $(".delUser").click(function () {
        // 判断当前所选数据的数目
        var checkStatus = table.checkStatus('userListTable');
        var length = checkStatus.data.length;
        if (length == 0) {
            layer.msg("请选择一条需要删除的内容", {time: 1000});
            return;
        }
        var id = checkStatus.data[0].id;

        layer.confirm('确定删除？', {icon: 3, title: '提示'}, function () {
            $.ajax({
                type: "DELETE",
                url: "../user/" + id,
                success: function (res) {
                    if (parseInt(res.obj) > 0) {
                        layer.msg(
                            '删除成功',
                            {
                                icon: 1,
                                time: 1000
                            },
                            function () {
                                window.location.reload();
                            });
                    } else {
                        layer.msg('删除失败', {icon: 2});
                    }
                },
                error: function (o) {
                }
            });
        });
    });

    //更新按钮监听
    $(".updateUser").click(function () {
        //判断当前所选数据的数目
        var checkStatus = table.checkStatus('userListTable');
        var length = checkStatus.data.length;
        if (length == 0) {
            layer.msg("请选择一条需要编辑的内容", {time: 1000});
            return;
        }
        var data = checkStatus.data[0];
        //屏幕可用工作区宽
        const clientWidth = window.screen.availWidth;
        const wid = clientWidth + "px";
        //屏幕可用工作区高
        const clientHeight = window.screen.availHeight;
        const hei = clientHeight + "px";
        var index = layer.open({
            title: "修改用户",
            type: 2,
            anim: 5,
            area: [wid, hei],
            content: "personal-edit.html",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
                $(document).find("input[type=radio]").val(data.role);
                comExt.fillInput(body, data);
            }
        });
        comExt.full(index);
        /* 渲染表单 */
        form.render();
    });

    //新增按钮监听
    $(".addUser").click(function () {
        //屏幕可用工作区宽
        const clientWidth = window.screen.availWidth;
        const wid = clientWidth + "px";
        //屏幕可用工作区高
        const clientHeight = window.screen.availHeight;
        const hei = clientHeight + "px";
        var index = layer.open({
            title: "新增用户",
            type: 2,
            anim: 5,
            area: [wid, hei],
            content: "personal-add.html",
            success: function (layero, index) {
                var body = layui.layer.getChildFrame('body', index);
            }
        });
        comExt.full(index);
        /* 渲染表单 */
        form.render();
    });


});